﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CotrobaieStatistics
{  
    /// <summary>
    /// Reprezinta o linie de regresie printre preturile observate, de tipul pret = a*timp + b
    /// </summary>
    struct RegressionLine
    {
        private double _a, _b;
        public double a { get { return _a; } set { _a = value; } }
        public double b { get { return _b; } set { _b = value; } }
        public RegressionLine(double a, double b)
        {
            _a = a;
            _b = b;
        }
        public RegressionLine(double[] x, double[] y)
        {
            _a = 0;
            _b = 0;
            BuildLine(x, y);
        }


        private void BuildLine(double[] x, double[] y)
        {
            int n = x.Length;
            double sx=0, sy=0, sxy=0,ssqx = 0;
            for (int i = 0; i < n; i++)
            {
                sx += x[i];
                sy += y[i];
                sxy += x[i] * y[i];
                ssqx += x[i] * x[i];
            }
            double num = sx*sx - n* ssqx;
            _a = (sx * sy - n * sxy) / num;
            _b = (sx * sxy - ssqx * sy) / num;

        }
    }
}
